In first place, we have to load all the packages we will use.
library(quantmod)
library(PerformanceAnalytics)
library(dygraphs)
library(timeDate)
library(d3heatmap)
library(shiny)
Note: I used some new libraries like d3heatmap and a complementary shiny. It’s a library for interactive heat graph.
We will start downloading the data of stock exchange EUROSTOXX 50,known at YaHoo Financial as STOXX50E and several Prices as well.
After downloading we plot the data.
getSymbols("^STOXX50E", from='2000-01-01')
## As of 0.4-0, 'getSymbols' uses env=parent.frame() and
## auto.assign=TRUE by default.
##
## This behavior will be phased out in 0.5-0 when the call will
## default to use auto.assign=FALSE. getOption("getSymbols.env") and
## getOptions("getSymbols.auto.assign") are now checked for alternate defaults
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for more details.
## [1] "STOXX50E"
STOXX50E <- STOXX50E[complete.cases(STOXX50E),]
head(STOXX50E)
## STOXX50E.Open STOXX50E.High STOXX50E.Low STOXX50E.Close
## 2000-01-03 4849.22 4849.22 4849.22 4849.22
## 2000-01-04 4657.83 4657.83 4657.83 4657.83
## 2000-01-05 4541.75 4541.75 4541.75 4541.75
## 2000-01-06 4500.69 4500.69 4500.69 4500.69
## 2000-01-07 4648.27 4648.27 4648.27 4648.27
## 2000-01-10 4714.03 4714.03 4714.03 4714.03
## STOXX50E.Volume STOXX50E.Adjusted
## 2000-01-03 0 4849.22
## 2000-01-04 0 4657.83
## 2000-01-05 0 4541.75
## 2000-01-06 0 4500.69
## 2000-01-07 0 4648.27
## 2000-01-10 0 4714.03
names(STOXX50E)
## [1] "STOXX50E.Open" "STOXX50E.High" "STOXX50E.Low"
## [4] "STOXX50E.Close" "STOXX50E.Volume" "STOXX50E.Adjusted"
chartSeries(STOXX50E, type = c("auto", "line"), theme = chartTheme('white',up.col='dodgerblue3',dn.col='dodgerblue4'))
In this section we download the data of several prices from STOXX50E:
getSymbols(c("ASML.AS", "BAYN.DE", "BNP.PA", "DTE.DE"), from='2000-01-01')
## [1] "ASML.AS" "BAYN.DE" "BNP.PA" "DTE.DE"
Now we plot this prices and add some indicators: * addVo- adds volume indicator * addBBands- adds Bollinger Bands indicator * addCCI- Commodity Channel Index indicator
chartSeries(ASML.AS, theme=chartTheme('white',up.col='dodgerblue',dn.col='dodgerblue4'),
subset='last 6 months', TA="addVo();addBBands();addCCI()", type = "line", name ="ASML Holding N.V.")
chartSeries(BAYN.DE, theme=chartTheme('white', up.col='dodgerblue',dn.col='dodgerblue4'),
subset='last 6 months', TA="addVo();addBBands();addCCI()", type = "line", name="Bayer AG")
chartSeries(BNP.PA, theme=chartTheme('white', up.col='dodgerblue',dn.col='dodgerblue4'),
subset='last 6 months', TA="addVo();addBBands();addCCI()", type = "line", name= "BNP Paribas S.A.")
chartSeries(DTE.DE, theme=chartTheme('white', up.col='dodgerblue',dn.col='dodgerblue4'),
subset='last 6 months', TA="addVo();addBBands();addCCI()", type = "line", name="Deutsche Telekom AG")
Now we are merging the data, creating a new data set in which all the variable have the same dates, also we omit all NAs because of future problems with the calculus.
newdata <- merge(STOXX50E$STOXX50E.Adjusted, ASML.AS$ASML.AS.Adjusted, BAYN.DE$BAYN.DE.Adjusted,BNP.PA$BNP.PA.Adjusted, DTE.DE$DTE.DE.Adjusted)
newdata <- na.omit(newdata)
names(newdata)
## [1] "STOXX50E.Adjusted" "ASML.AS.Adjusted" "BAYN.DE.Adjusted"
## [4] "BNP.PA.Adjusted" "DTE.DE.Adjusted"
Now we change the names of all prices
names(newdata) <- c("STOXX50E","ASML.AS", "BAYN.DE", "BNP.PA", "DTE.DE")
the next step is the extraction of the elements
STOXX50E <- newdata$STOXX50E
ASML.AS <- newdata$ASML.AS
BAYN.DE <- newdata$BAYN.DE
BNP.PA <- newdata$BNP.PA
DTE.DE <- newdata$DTE.DE
Return:
\[ R_t = \dfrac{P_t - P_{t-1}}{P_{t-1}} = \dfrac{P_t}{P_{t-1}} - 1\] Continuously compounded return (log-return)
\[ r_t = \Delta \log P_t = \log(P_t) - \log(P_{t-1}) = \log \dfrac{P_t}{P_{t-1}}\]
Relationship \[ R_t = \exp(r_t) - 1\]
Now we calculate the Return and Continuously compounded return applying the definitions.
This is a basic financial package we can use to calculate returns. There is another financial package called PerformanceAnalytics that includes a command to calculate returns as well.
RSTOXX50E <- Delt(STOXX50E, type="arithmetic")
rSTOXX50E <- Delt(STOXX50E, type="log")
head(rSTOXX50E)
## Delt.1.log
## 2000-01-03 NA
## 2000-01-04 -0.040268193
## 2000-01-05 -0.025237274
## 2000-01-06 -0.009081682
## 2000-01-07 0.032264389
## 2000-01-10 0.014048061
head(RSTOXX50E)
## Delt.1.arithmetic
## 2000-01-03 NA
## 2000-01-04 -0.039468203
## 2000-01-05 -0.024921476
## 2000-01-06 -0.009040568
## 2000-01-07 0.032790528
## 2000-01-10 0.014147199
head(exp(rSTOXX50E) - 1)
## Delt.1.log
## 2000-01-03 NA
## 2000-01-04 -0.039468203
## 2000-01-05 -0.024921476
## 2000-01-06 -0.009040568
## 2000-01-07 0.032790528
## 2000-01-10 0.014147199
Thanks to merging data with merge command we can simultaneously calculate the returns for all the assets of the data frame.
rdata <- Return.calculate(newdata, method="log")
Rdata <- Return.calculate(newdata, method="discrete")
head(rdata)
## STOXX50E ASML.AS BAYN.DE BNP.PA DTE.DE
## 2000-01-03 NA NA NA NA NA
## 2000-01-04 -0.040268193 -0.04920362 -0.02692636 -0.027951280 -0.03665563
## 2000-01-05 -0.025237274 -0.10022938 -0.02553927 -0.021916460 -0.02644459
## 2000-01-06 -0.009081682 -0.07251116 0.02896372 -0.034686027 -0.06735163
## 2000-01-07 0.032264389 0.07200528 0.04080880 0.012273361 0.05950161
## 2000-01-10 0.014048061 0.06953384 0.03568677 0.006374103 0.02320716
head(Rdata)
## STOXX50E ASML.AS BAYN.DE BNP.PA DTE.DE
## 2000-01-03 NA NA NA NA NA
## 2000-01-04 -0.039468203 -0.04801273 -0.02656708 -0.027564257 -0.03599195
## 2000-01-05 -0.024921476 -0.09537011 -0.02521590 -0.021678039 -0.02609799
## 2000-01-06 -0.009040568 -0.06994463 0.02938725 -0.034091362 -0.06513358
## 2000-01-07 0.032790528 0.07466101 0.04165292 0.012348988 0.06130747
## 2000-01-10 0.014147199 0.07200834 0.03633118 0.006394461 0.02347855
rdata <- na.omit(rdata)
Rdata <- na.omit(Rdata)
names(rdata) <- c("rSTOXX50E","rASML.AS", "rBAYN.DE", "rBNP.PA", "rDTE.DE")
names(rdata)
## [1] "rSTOXX50E" "rASML.AS" "rBAYN.DE" "rBNP.PA" "rDTE.DE"
rSTOXX50E <- rdata$rSTOXX50E
rASML.AS <- rdata$rASML.AS
rBAYN.DE <- rdata$rBAYN.DE
rBNP.PA <- rdata$rBNP.PA
rDTE.DE <- rdata$rDTE.DE
Correlations are a linear concept \[ y = \beta x + u\]
Then \(\beta\) is proportional to the correlation between \(x\) and \(y\)
A different way to say that is linear dependence
The relationship between the two variables is always the same regardless of the magnitude of the variables
Under the normal distribution, dependence is linear.
Using the forecast library we can plot
library(forecast)
## This is forecast 7.3
tsdisplay(rSTOXX50E, main = "STOXX50E")
tsdisplay(rASML.AS, main = "ASML Holding NV")
tsdisplay(rBAYN.DE, main = "Bayer AG")
tsdisplay(rBNP.PA, main = "BNP Paribas S.A.")
tsdisplay(rDTE.DE, main ="Deutsche Telekom AG")
Generate correlation matrix
Rmatrix <- as.matrix(Rdata[-1,])
M <- cor(Rmatrix, use="complete.obs")
M
## STOXX50E ASML.AS BAYN.DE BNP.PA DTE.DE
## STOXX50E 1.0000000 0.5776573 0.5611880 0.7699548 0.6354233
## ASML.AS 0.5776573 1.0000000 0.3128641 0.3851108 0.4313222
## BAYN.DE 0.5611880 0.3128641 1.0000000 0.3981802 0.3893523
## BNP.PA 0.7699548 0.3851108 0.3981802 1.0000000 0.3897968
## DTE.DE 0.6354233 0.4313222 0.3893523 0.3897968 1.0000000
M <- round(M,4)
M
## STOXX50E ASML.AS BAYN.DE BNP.PA DTE.DE
## STOXX50E 1.0000 0.5777 0.5612 0.7700 0.6354
## ASML.AS 0.5777 1.0000 0.3129 0.3851 0.4313
## BAYN.DE 0.5612 0.3129 1.0000 0.3982 0.3894
## BNP.PA 0.7700 0.3851 0.3982 1.0000 0.3898
## DTE.DE 0.6354 0.4313 0.3894 0.3898 1.0000
Generate heatmap
d3heatmap(M, dendrogram = "none",
color = "Blues", title="Correlation Heatmap")
Heatmap shows the correlations between all inputs and the dark blue means that there is a high level of correlation and the clear ones the oposit.
In this section we will try to assess the constancy of correlation over the time. We will use the log-returns of the prices. 1. First step is the extraction of the related data from our Rmatrix (Correlation matrix). 2. Then we array with dim, this attribute is an integer vector of length one or more containing non-negative values. 3. We define the k dimension. Recursive correlation starting at observation 100. 4. Apply our dimension to vectors. 5. Calculate the correlation. 6. Create xts object from the data we prepared. 7. Plot the recursive correlation.
The next step consist in modify this approach. We will calculate the correlation between the sub vectors of a fixed size of the matrix (N=100). This is what we call the rolling approach.
The importance of this demostratios is to show that the correlation is not constant over the time, so the investors can see that their portfolio has to be adapted by the changes in the correlation between the portfolio componets and thereference index.
As we can see on the graphs down below almost all the Recursive COrrelations increased between 2001 and 2004. The exception is the relation ASML.AS-DTE.DE witch decreased in this period. There is an change on the correlation series around the financial crisis period as well. This information is crusial for the creation of the portfolio.
mX23 <- Rmatrix[,2]
mY23 <- Rmatrix[,3]
k0 <- 99
T <- length(mX23)
cXY23 <- array(dim=(T-k0))
for(i in 1:(T-k0))
{
k <- k0 + i
vX23 <- mX23[1:k]
vY23 <- mY23[1:k]
cXY23[i] <- cor(vX23, vY23)
}
cXY23 <- xts(cXY23, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY23, main = "Recursive Correlation: ASML.AS-BAYN.DE") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)%>%
dyShading(from = "2000-05-26", to = "2004-01-01", color = "#FFE6E6") %>%
dyShading(from = "2008-09-01", to = "2010-01-01", color = "#FFE6E6")
N <- 100
cXY23 <- array(dim=(T-N+1))
for(i in 1:(T-N+1))
{
vX23 <- mX23[i:(i+N)]
vY23 <- mY23[i:(i+N)]
cXY23[i] <- cor(vX23,vY23)
}
cXY23 <- xts(cXY23, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY23, main = "Rolling Correlation: ASML.AS-BAYN.DE") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)
mX24 <- Rmatrix[,2]
mY24 <- Rmatrix[,4]
k0 <- 99
T <- length(mX24)
cXY24 <- array(dim=(T-k0))
for(i in 1:(T-k0))
{
k <- k0 + i
vX24 <- mX24[1:k]
vY24 <- mY24[1:k]
cXY24[i] <- cor(vX24, vY24)
}
cXY24 <- xts(cXY24, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY24, main = "Recursive Correlation: ASML.AS-BNP.PA") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)%>%
dyShading(from = "2000-05-26", to = "2004-01-01", color = "#FFE6E6") %>%
dyShading(from = "2008-09-01", to = "2010-01-01", color = "#FFE6E6")
N <- 100
cXY24 <- array(dim=(T-N+1))
for(i in 1:(T-N+1))
{
vX24 <- mX24[i:(i+N)]
vY24 <- mY24[i:(i+N)]
cXY24[i] <- cor(vX24,vY24)
}
cXY24 <- xts(cXY24, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY24, main = "Rolling Correlation: ASML.AS-BNP.PA") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)
mX25 <- Rmatrix[,2]
mY25 <- Rmatrix[,5]
k0 <- 99
T <- length(mX25)
cXY25 <- array(dim=(T-k0))
for(i in 1:(T-k0))
{
k <- k0 + i
vX25 <- mX25[1:k]
vY25 <- mY25[1:k]
cXY25[i] <- cor(vX25, vY25)
}
cXY25 <- xts(cXY25, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY25, main = "Recursive Correlation: ASML.AS-DTE.DE") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)%>%
dyShading(from = "2000-05-26", to = "2004-01-01", color = "#FFE6E6") %>%
dyShading(from = "2008-09-01", to = "2010-01-01", color = "#FFE6E6")
N <- 100
cXY25 <- array(dim=(T-N+1))
for(i in 1:(T-N+1))
{
vX25 <- mX25[i:(i+N)]
vY25 <- mY25[i:(i+N)]
cXY25[i] <- cor(vX25,vY25)
}
cXY25 <- xts(cXY25, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY25, main = "Rolling Correlation: ASML.AS-DTE.DE") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)
mX34 <- Rmatrix[,3]
mY34 <- Rmatrix[,4]
k0 <- 99
T <- length(mX34)
cXY34 <- array(dim=(T-k0))
for(i in 1:(T-k0))
{
k <- k0 + i
vX34 <- mX34[1:k]
vY34 <- mY34[1:k]
cXY34[i] <- cor(vX34, vY34)
}
cXY34 <- xts(cXY34, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY34, main = "Recursive Correlation: BAYN.DE-BNP.PA") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)%>%
dyShading(from = "2000-05-26", to = "2004-01-01", color = "#FFE6E6") %>%
dyShading(from = "2008-09-01", to = "2010-01-01", color = "#FFE6E6")
N <- 100
cXY34 <- array(dim=(T-N+1))
for(i in 1:(T-N+1))
{
vX34 <- mX34[i:(i+N)]
vY34 <- mY34[i:(i+N)]
cXY34[i] <- cor(vX34,vY34)
}
cXY34 <- xts(cXY34, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY34, main = "Rolling Correlation: BAYN.DE-BNP.PA") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)
mX35 <- Rmatrix[,3]
mY35 <- Rmatrix[,5]
k0 <- 99
T <- length(mX35)
cXY35 <- array(dim=(T-k0))
for(i in 1:(T-k0))
{
k <- k0 + i
vX35 <- mX35[1:k]
vY35 <- mY35[1:k]
cXY35[i] <- cor(vX35, vY35)
}
cXY35 <- xts(cXY35, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY35, main = "Recursive Correlation: BAYN.DE-DTE.DE") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)%>%
dyShading(from = "2000-05-26", to = "2004-01-01", color = "#FFE6E6") %>%
dyShading(from = "2008-09-01", to = "2010-01-01", color = "#FFE6E6")
N <- 100
cXY35 <- array(dim=(T-N+1))
for(i in 1:(T-N+1))
{
vX35 <- mX35[i:(i+N)]
vY35 <- mY35[i:(i+N)]
cXY35[i] <- cor(vX35,vY35)
}
cXY35 <- xts(cXY35, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY35, main = "Rolling Correlation: BAYN.DE-DTE.DE") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)
mX45 <- Rmatrix[,4]
mY45 <- Rmatrix[,5]
k0 <- 99
T <- length(mX45)
cXY45 <- array(dim=(T-k0))
for(i in 1:(T-k0))
{
k <- k0 + i
vX45 <- mX45[1:k]
vY45 <- mY45[1:k]
cXY45[i] <- cor(vX45, vY45)
}
cXY45 <- xts(cXY45, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY45, main = "Recursive Correlation: BNP.PA-DTE.DE") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)%>%
dyShading(from = "2000-05-26", to = "2004-01-01", color = "#FFE6E6") %>%
dyShading(from = "2008-09-01", to = "2010-01-01", color = "#FFE6E6")
N <- 100
cXY45 <- array(dim=(T-N+1))
for(i in 1:(T-N+1))
{
vX45 <- mX45[i:(i+N)]
vY45 <- mY45[i:(i+N)]
cXY45[i] <- cor(vX45,vY45)
}
cXY45 <- xts(cXY45, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY45, main = "Rolling Correlation: BNP.PA-DTE.DE") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)
Now we check the correlation between the Prices and STOXX50.
As we can see on the graphs below there are two cases of the correlation changes over the time. 1. Corrlation between the index and ASML & DTE; the correlation decreased since 2004 in ASML case and since 2000 in case of Deutsche Telekom. 2. Corrlation between the index and BAYN & BNP; the correlation increased since 2001 very fastly and later got stabilized on high values.
mX12 <- Rmatrix[,1]
mY12 <- Rmatrix[,2]
k0 <- 99
T <- length(mX12)
cXY12 <- array(dim=(T-k0))
for(i in 1:(T-k0))
{
k <- k0 + i
vX12 <- mX12[1:k]
vY12 <- mY12[1:k]
cXY12[i] <- cor(vX12, vY12)
}
cXY12 <- xts(cXY12, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY12, main = "Recursive Correlation: STOXX50- ASML.AS") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)%>%
dyShading(from = "2000-05-26", to = "2004-01-01", color = "#FFE6E6") %>%
dyShading(from = "2008-09-01", to = "2010-01-01", color = "#FFE6E6")
N <- 100
cXY12 <- array(dim=(T-N+1))
for(i in 1:(T-N+1))
{
vX12 <- mX12[i:(i+N)]
vY12 <- mY12[i:(i+N)]
cXY12[i] <- cor(vX12,vY12)
}
cXY12 <- xts(cXY12, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY12, main = "Rolling Correlation: STOXX50- ASM.AS") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)
mX13 <- Rmatrix[,1]
mY13 <- Rmatrix[,3]
k0 <- 99
T <- length(mX13)
cXY13 <- array(dim=(T-k0))
for(i in 1:(T-k0))
{
k <- k0 + i
vX13 <- mX13[1:k]
vY13 <- mY13[1:k]
cXY13[i] <- cor(vX13, vY13)
}
cXY13 <- xts(cXY13, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY13, main = "Recursive Correlation: STOXX50- BAYN.DE") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)%>%
dyShading(from = "2000-05-26", to = "2004-01-01", color = "#FFE6E6") %>%
dyShading(from = "2008-09-01", to = "2010-01-01", color = "#FFE6E6")
N <- 100
cXY13 <- array(dim=(T-N+1))
for(i in 1:(T-N+1))
{
vX13 <- mX13[i:(i+N)]
vY13 <- mY13[i:(i+N)]
cXY13[i] <- cor(vX13,vY13)
}
cXY13 <- xts(cXY13, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY13, main = "Rolling Correlation: STOXX50- BAYN.DE") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)
mX14 <- Rmatrix[,1]
mY14 <- Rmatrix[,4]
k0 <- 99
T <- length(mX14)
cXY14 <- array(dim=(T-k0))
for(i in 1:(T-k0))
{
k <- k0 + i
vX14 <- mX14[1:k]
vY14 <- mY14[1:k]
cXY14[i] <- cor(vX14, vY14)
}
cXY14 <- xts(cXY14, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY14, main = "Recursive Correlation: STOXX50- BNP.PA") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)%>%
dyShading(from = "2000-05-26", to = "2004-01-01", color = "#FFE6E6") %>%
dyShading(from = "2008-09-01", to = "2010-01-01", color = "#FFE6E6")
N <- 100
cXY14 <- array(dim=(T-N+1))
for(i in 1:(T-N+1))
{
vX14 <- mX14[i:(i+N)]
vY14 <- mY14[i:(i+N)]
cXY14[i] <- cor(vX14,vY14)
}
cXY14 <- xts(cXY14, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY14, main = "Rolling Correlation: STOXX50- BNP.PA") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)
mX15 <- Rmatrix[,1]
mY15 <- Rmatrix[,5]
k0 <- 99
T <- length(mX15)
cXY15 <- array(dim=(T-k0))
for(i in 1:(T-k0))
{
k <- k0 + i
vX15 <- mX15[1:k]
vY15 <- mY15[1:k]
cXY15[i] <- cor(vX15, vY15)
}
cXY15 <- xts(cXY15, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY15, main = "Recursive Correlation: STOXX50- DTE.DE") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)%>%
dyShading(from = "2000-05-26", to = "2004-01-01", color = "#FFE6E6") %>%
dyShading(from = "2008-09-01", to = "2010-01-01", color = "#FFE6E6")
N <- 100
cXY15 <- array(dim=(T-N+1))
for(i in 1:(T-N+1))
{
vX15 <- mX15[i:(i+N)]
vY15 <- mY15[i:(i+N)]
cXY15[i] <- cor(vX15,vY15)
}
cXY15 <- xts(cXY15, index(rSTOXX50E)[(k0+2):(T+1)])
dygraph(cXY15, main = "Rolling Correlation: STOXX50- DTE.DE") %>% dyRangeSelector(dateWindow = c("2008-01-01", "2017-02-01"))%>%
dyOptions(fillGraph = TRUE, fillAlpha = 0.2)